﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace HeathCareSystem.wuc
{
    public partial class wucDangNhap : System.Web.UI.UserControl
    {
        DataProvider d = new DataProvider();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["isLogin"] == null || (bool)Session["isLogin"] == false)
            {
                pnlIsLogin.Visible = false;
                pnlLogin.Visible = true;
            }
            else
            {
                pnlIsLogin.Visible = true;
                pnlLogin.Visible = false;     
            }
        }

        protected void btnDangNhap_Click(object sender, EventArgs e)
        {
            String strSQL = "SELECT * FROM TaiKhoan WHERE (TenDangNhap = '" + txtTaiKhoan.Text + "') AND (MatKhau = '" + txtMatKhau.Text + "')";

            DataTable dt = d.ExecuteQuery(strSQL);
            if (dt.Rows.Count > 0)
            {
                Session["isLogin"] = true;
                Session["MaTaiKhoan"] = dt.Rows[0]["MaTaiKhoan"].ToString();
                Session["TenDangNhap"] = dt.Rows[0]["TenDangNhap"].ToString();
                Session["MaLoaiTaiKhoan"] = dt.Rows[0]["MaLoaiTaiKhoan"].ToString();
                string tmp = Session["MaLoaiTaiKhoan"].ToString();
                if (Session["MaLoaiTaiKhoan"].ToString() == "LTK03")
                {
                    ThamSoDTO ts = new ThamSoDTO();
                    strSQL = string.Format("Select * from thamso");
                    DataTable dt1 = d.ExecuteQuery(strSQL);
                    if (dt1.Rows.Count > 0)
                    {                        
                        ts.Ngay = DateTime.Parse(dt1.Rows[0]["Ngay"].ToString());
                        ts.FirstTime = bool.Parse(dt1.Rows[0]["FirstTime"].ToString());
                        if (DateTime.Now > ts.Ngay)
                        {
                            ts.FirstTime = true;
                            ts.Ngay = DateTime.Now;
                            new ThamSoDAO().CapNhatThamSo(ts);
                            new BenhNhanTrongNgayBUS().XoaDanhSachBenhNhanTrongNgay();
                        }
                    }
                    else
                    {
                        ts.FirstTime = true;
                        ts.Ngay = DateTime.Now;
                        new ThamSoDAO().ThemThamSo(ts);
                    }
                }

                if (bool.Parse(dt.Rows[0]["DangNhapLanDau"].ToString()) == false)
                {
                    Response.Redirect("~/Default.aspx?Page=ThayDoiMatKhau");
                    pnlIsLogin.Visible = false;
                    pnlLogin.Visible = true;
                }
                else
                {
                    Response.Redirect("~/Default.aspx");
                    pnlIsLogin.Visible = false;
                    pnlLogin.Visible = true;
                }
            }
            else
                lblError.Text = "Tên Đăng Nhập hay Mật Khẩu sai";
        }

        protected void btnThoat0_Click(object sender, EventArgs e)
        {
            Session["isLogin"] = Session["isLogin"] = Session["MaTaiKhoan"] =
            Session["TenDangNhap"] = Session["MaLoaiTaiKhoan"] = null;
            Response.Redirect("~/Default.aspx");
        }
    }
}